//
//  DataHandel.m
//  Days
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 dss. All rights reserved.
//

#import "DataHandel.h"
#import <CoreLocation/CoreLocation.h>

@interface DataHandel ()<CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager *locationManager;

@end


static DataHandel *_dataHandel = nil;
@implementation DataHandel
+(instancetype)sharedHandel{
    return [[self alloc]init];
    
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_dataHandel == nil) {
            _dataHandel  =[super allocWithZone:zone];
        }
    });
    return _dataHandel;
}

//根据网址得到数据的方法
-(void)getDataWithURL:(NSString *)url withBlock:(GETBLOCK) block{
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *datatask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            block(object);
            
        });
        
    }];
    
    
    //执行任务
    [datatask resume];
    
    
}





//开始定位
-(void)startLocation{
    if (self.locationManager == nil) {
        self.locationManager =[[ CLLocationManager alloc]init];
    
    }
    //设置代理
    self.locationManager.delegate  = self;
    //请求用户允许使用定位
    [self.locationManager requestWhenInUseAuthorization];
    //定位精度
    CLLocationDistance distance = 100;
    self.locationManager.distanceFilter = distance;
    
    //定位质量
    
    self.locationManager.desiredAccuracy =kCLLocationAccuracyThreeKilometers;
    
    
    //判断定位服务是否可用 并且用户是否允许定位
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied ) {
        
    }
    
    //iOS7 以前
    //开启定位服务,开始更新位置
    [self.locationManager startUpdatingLocation];

    
}
#pragma mark -- 逆地理编码(根据经纬度获取地址)
-(void)getAddressWithCoordinate:(CLLocationCoordinate2D )coordinate withFinish:(unGeographyCodingBlock)finishBlock{
    
    //编码类
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    //根据CLLocationCoordinate2D 构造一个 CLLocation
    CLLocation *location = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    
    
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //拿出第一个元素
            CLPlacemark *mark = placemarks.firstObject;
            finishBlock(mark.name);
         
        
        });
        
        
    }];

}

#pragma mark -- 定位失败
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    NSLog(@"定位失败");
    
}

#pragma mark -- 定位成功
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *location = locations.firstObject;
    
    CLLocationCoordinate2D coordinate = location.coordinate;
    
    if (self.updateBlock) {
        self.updateBlock(coordinate);
    }
}


@end
